package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.CategoriesBo;
import com.ruoyi.supplier.domain.vo.CategoriesVo;
import com.ruoyi.supplier.domain.Categories;
import com.ruoyi.supplier.mapper.CategoriesMapper;
import com.ruoyi.supplier.service.ICategoriesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper,Categories> implements ICategoriesService {

    private final CategoriesMapper baseMapper;

    /**
     * 查询商品分类
     */
    @Override
    public CategoriesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品分类列表
     */
    @Override
    public TableDataInfo<CategoriesVo> queryPageList(CategoriesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Categories> lqw = buildQueryWrapper(bo);
        Page<CategoriesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品分类列表
     */
    @Override
    public List<CategoriesVo> queryList(CategoriesBo bo) {
        LambdaQueryWrapper<Categories> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Categories> buildQueryWrapper(CategoriesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Categories> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPid() != null, Categories::getPid, bo.getPid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Categories::getName, bo.getName());
        lqw.eq(bo.getLevel() != null, Categories::getLevel, bo.getLevel());
        lqw.eq(bo.getSort() != null, Categories::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), Categories::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), Categories::getPath, bo.getPath());
        lqw.eq(bo.getForAudit() != null, Categories::getForAudit, bo.getForAudit());
        lqw.eq(bo.getStatus() != null, Categories::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增商品分类
     */
    @Override
    public Boolean insertByBo(CategoriesBo bo) {
        Categories add = BeanUtil.toBean(bo, Categories.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品分类
     */
    @Override
    public Boolean updateByBo(CategoriesBo bo) {
        Categories update = BeanUtil.toBean(bo, Categories.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Categories entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
